require 'spec_helper'
require 'osx/cocoa'
require 'macos/main_view'
include OSX

describe STMainView, "when running" do
  before(:each) do    
    @view = STMainView.alloc.init
    @view.stub!(:bounds).and_return(NSMakeRect(0, 0, 200, 200))
    @controller = mock("GameController", :null_object => true)
    
    @view.controller = @controller
  end
  
  it "should call #tick on timer firing" do
    @view.should_receive(:tick).once
    
    @view.awakeFromNib
    @view.timer.fire
  end
  
  it "should redraw the screen when updated" do
    @view.should_receive(:setNeedsDisplay).once.with(true)
    
    @view.update
  end
  
  it "should fill itself with black on #drawSpace" do
    NSColor.blackColor.should_receive(:set)
    @view.should_receive(:NSRectFill).with(@view.bounds)
    
    @view.drawSpace
  end
  
  it "should draw each object on screen using the renderer" do
    @controller.game = mock("Game")
    @controller.game.stub!(:objects).and_return([GameObject.load("example_object"), GameObject.load("player"), GameObject.load("planet")])
    
    @view.renderer = mock("CocoaRenderer")
    @view.renderer.should_receive(:render).exactly(3).times
    
    @view.drawObjects
  end
  
  it "should proxy keyDown and keyUp events to controller" do
    event = mock("KeyEvent")
    event.stub!(:keyCode).and_return(KEY_UP_ARROW)
    
    @controller.should_receive(:keyDown).once.with(event)
    @controller.should_receive(:keyUp).once.with(event)
    
    @view.keyDown(event)
    @view.keyUp(event)
  end
end